﻿using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace DAL.DBManagers
{
    public class BanksManager : BaseManager<Bank>
    {
        public BanksManager()
            : base()
        {
        }

        public List<BankHelper> GetBanks()
        {
            return base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new BankHelper
                {
                    RecordID = s.RecordID,
                    ID = s.BankID,
                    Name = s.Name,
                    NameGeo = s.NameGeo,
                    BeginDate = s.BeginDate,
                    FinishDate = s.FinishDate,
                    IsActive = s.IsActive
                }).ToList();
        }

        public List<BankHelper> GetBanks(string sortBy)
        {
            List<BankHelper> prodList = base.GetAll()
                       .Where(w => w.IsActive && w.FinishDate == null).Select(s => new BankHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.BankID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<BankHelper> cmp = new DAL.Models.GenericComparer<BankHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public List<BankHelper> GetBanks(Guid BankID)
        {
            return base.GetAll().Where(w => w.IsActive && w.BankID == BankID)
                       .Select(s => new BankHelper
                          {
                              RecordID = s.RecordID,
                              ID = s.BankID,
                              Name = s.Name,
                              NameGeo = s.NameGeo,
                              BeginDate = s.BeginDate,
                              FinishDate = s.FinishDate,
                              IsActive = s.IsActive
                          }).OrderByDescending(o => o.BeginDate).ToList();
        }

        public List<BankHelper> GetBanks(Guid BankID, string sortBy)
        {
            var prodList = base.GetAll().Where(w => w.IsActive && w.BankID == BankID)
                       .Select(s => new BankHelper
                       {
                           RecordID = s.RecordID,
                           ID = s.BankID,
                           Name = s.Name,
                           NameGeo = s.NameGeo,
                           BeginDate = s.BeginDate,
                           FinishDate = s.FinishDate,
                           IsActive = s.IsActive
                       }).OrderByDescending(o => o.BeginDate).ToList();
            if (sortBy != "")
            {
                DAL.Models.GenericComparer<BankHelper> cmp = new DAL.Models.GenericComparer<BankHelper>(sortBy);
                prodList.Sort(cmp);
            }
            return prodList;
        }

        public void RepiarDates(Guid BankID)
        {
            var banks = this.Find(f => f.IsActive && f.BankID == BankID).OrderBy(o => o.BeginDate).ToList();

            var count = banks.Count();

            for (int i = 0; i < count; i++)
            {
                if (i < count - 1)
                {
                    if (banks[i].FinishDate != banks[i + 1].BeginDate)
                    {
                        banks[i].FinishDate = banks[i + 1].BeginDate;
                    }
                }
                else if (i == count - 1)
                {
                    if (banks[i].FinishDate != null)
                    {
                        banks[i].FinishDate = null;
                    }
                }
            }

            SaveChanges();
        }
    }
}
